import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * http请求重定向
 */
public class S18_DownServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //application/x-tar 弹出下载窗口，模拟下载
        resp.setContentType("application/x-tar;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print("hi");
        out.flush();
        out.close();

        //完成了自己的业务，发出请求重定向 url-pattern
        //1.sendRedirect() 本质会返回 302 状态码，同时和一个Location:/servlet/downServletNew
        //2.浏览器解析如上信息，在解析Location时 -> http://localhost:8080/servlet/downServletNew
        resp.sendRedirect("/servlet/downServletNew");
    }
}
